//
//  ProductDetailHeader.m
//  UEAssistant
//
//  Created by mac on 2017/2/10.
//  Copyright © 2017年 youyixinxi. All rights reserved.
//

#import "ProductDetailHeader.h"

@implementation ProductDetailHeader{
    BOOL                            _created;/**< 是否创建过 */
    BOOL                            _canFold;/**< 是否可展开 */
    BOOL                            _canSelect;/**< 是否可选择 */
    BOOL                            _flag;
    BOOL                            _ifhuadong;
    UIImageView                     *_typeImage;/**< 文件类型图片 */
    UILabel                         *_titleLabel;/**< 标题 */
    UILabel                         *_detailLabel;/**< 详细内容 */
    UIImageView                     *_imageView;/**< 收起图标 */
    UIButton                        *_btn;/**< 收起按钮 */
    UIView                          *_line;/**< 下划线 */
    CGFloat                         _titleHeight;
    UIButton                        *_labelDelete;
    UIButton                        *_labelSplit;
    UIButton                        *_labelQuQ;
    UIView                          *_baseView;
    UIImageView                     *_seletImage;/**< 选中勾勾 */
    
}



- (void)setFoldSectionHeaderViewWithTitle:(NSString *)title detail:(NSString *)detail section:(NSInteger)section canFold:(BOOL)canFold canSelect:(BOOL)canSelect height:(CGFloat)height{
    
    _titleHeight = height;
    
    if (!_created) {
        [self creatUI];
        [self addGuesPathMehod];
    }
    
    _titleLabel.text            = title;
    _detailLabel.attributedText = [self attributeStringWith:detail];
    _section                    = section;
    _canFold                    = canFold;
    _canSelect                  = canSelect;
    if (_canSelect) {
//        TGLog(@"------YES");
    }else{
//        TGLog(@"------NO");
    }
    
    if (canFold) {
        _imageView.hidden = NO;
    } else {
        _imageView.hidden = YES;
    }
}
- (NSMutableAttributedString *)attributeStringWith:(NSString *)wordcount {
    NSString *str = [NSString stringWithFormat:@"%@字", wordcount];
    NSMutableAttributedString *ats = [[NSMutableAttributedString alloc] initWithString:str];
    NSRange range = [str rangeOfString:wordcount];
    [ats setAttributes:@{NSForegroundColorAttributeName:[UIColor lightGrayColor]} range:range];
    return ats;
}

- (void)creatUI {
    _created = YES;
    
    [self.contentView addSubview:self.seletImage];
    
    //自定义扩展按钮
    _labelDelete = [UIButton buttonWithType:UIButtonTypeSystem];
    _labelDelete.frame = CGRectMake(Screen_Width-80, 0, 80, _titleHeight+57);
    [_labelDelete setTitle:@"删除" forState:UIControlStateNormal];
    _labelDelete.titleLabel.font = [UIFont systemFontOfSize:FLEXIBLE(18)];
    _labelDelete.backgroundColor = RGB(232, 89, 91, 1);
    [_labelDelete setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _labelDelete.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_labelDelete addTarget:self action:@selector(extensionButtonCliced:) forControlEvents:UIControlEventTouchUpInside];
    _labelDelete.tag = 920;
    [self.contentView addSubview:_labelDelete];
    
    _labelSplit = [UIButton buttonWithType:UIButtonTypeSystem];
    _labelSplit.frame = CGRectMake(Screen_Width-160, 0, 80, _titleHeight+57);
    [_labelSplit setTitle:@"拆分" forState:UIControlStateNormal];
    _labelSplit.titleLabel.font = [UIFont systemFontOfSize:FLEXIBLE(18)];
    _labelSplit.backgroundColor = RGB(57, 150, 219, 1);
    [_labelSplit setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _labelSplit.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_labelSplit addTarget:self action:@selector(extensionButtonCliced:) forControlEvents:UIControlEventTouchUpInside];
    _labelSplit.tag = 860;
    [self.contentView addSubview:_labelSplit];
    
     _labelQuQ = [UIButton buttonWithType:UIButtonTypeSystem];
    _labelQuQ.frame = CGRectMake(Screen_Width-240, 0, 80, _titleHeight+57);
    [_labelQuQ setTitle:@"去重" forState:UIControlStateNormal];
    _labelQuQ.titleLabel.font = [UIFont systemFontOfSize:FLEXIBLE(18)];
    _labelQuQ.backgroundColor = RGB(79, 209, 161, 1);
    [_labelQuQ setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _labelQuQ.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_labelQuQ addTarget:self action:@selector(extensionButtonCliced:) forControlEvents:UIControlEventTouchUpInside];
    _labelQuQ.tag = 800;
    [self.contentView addSubview:_labelQuQ];
    
    _baseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, _titleHeight+57)];
    _baseView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_baseView];
    
    //类型图片
    _typeImage = [[UIImageView alloc]initWithFrame:CGRectMake(15, 18, 50, 50)];
    _typeImage.image = [UIImage imageNamed:@"word"];
    [_baseView addSubview:_typeImage];
    
    //标题
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(_typeImage.frame.origin.x+_typeImage.frame.size.width+10, 18, Screen_Width - _typeImage.frame.size.width-20-70, _titleHeight)];
    _titleLabel.font = [UIFont systemFontOfSize:FLEXIBLE(18)];
    _titleLabel.backgroundColor = [UIColor clearColor];
    _titleLabel.textColor = [UIColor darkGrayColor];
    _titleLabel.numberOfLines = 0;
    [_baseView addSubview:_titleLabel];
    
    //其他内容
    _detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(_typeImage.frame.origin.x+_typeImage.frame.size.width+10, _titleLabel.frame.origin.y+_titleLabel.frame.size.height+5, 100, 15)];
    _detailLabel.font = [UIFont systemFontOfSize:FLEXIBLE(15)];
    _detailLabel.backgroundColor = [UIColor whiteColor];
    _detailLabel.textColor = [UIColor lightGrayColor];
    [_baseView addSubview:_detailLabel];
    
    //按钮
    _btn = [UIButton buttonWithType:UIButtonTypeCustom];
    _btn.frame = CGRectMake(Screen_Width-50, 18, 50, 50);
    [_btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_baseView addSubview:_btn];
    
    //图片
    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(Screen_Width - 40, 20*Radio+13, 20*Radio, 20*Radio)];
    _imageView.image = [UIImage imageNamed:@"unfold"];
    [_baseView addSubview:_imageView];
    
    //线
    _line = [[UIView alloc] initWithFrame:CGRectMake(0, 40 +18 + _titleHeight - 1, Screen_Width, 1)];
    _line.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    [_baseView addSubview:_line];
    
    _flag = YES;
    
}

- (void)setFold:(BOOL)fold {
    _fold = fold;
    if (fold) {
        
        [UIView animateWithDuration:3 animations:^{
//            _line.frame = CGRectMake(0, _typeImage.frame.origin.y+_typeImage.frame.size.height+13, Screen_Width, 0);
        }];
        _imageView.image = [UIImage imageNamed:@"unfold"];
    }
    else {
        [UIView animateWithDuration:3 animations:^{
//            _line.frame = CGRectMake(0, _typeImage.frame.origin.y+_typeImage.frame.size.height+13, Screen_Width, 1);
        }];
        _imageView.image = [UIImage imageNamed:@"more_2"];
    }
}

#pragma mark -- 折叠协议
- (void)btnClick:(UIButton *)btn {
    if (_canFold) {
        if ([self.delegate respondsToSelector:@selector(foldHeaderInSection:)]) {
            [self.delegate foldHeaderInSection:_section];
        }
    }
}

#pragma 扩展按钮协议
- (void)extensionButtonCliced:(UIButton *)sender{

    if ([self.delegate respondsToSelector:@selector(extensionButtonInSection:tag:)]) {
        [self.delegate extensionButtonInSection:_section tag:sender.tag];
    }
    [self animationToidenty];
}


#pragma mark -- 添加左滑手势
- (void)addGuesPathMehod{

    UISwipeGestureRecognizer * recognizer;
    recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
    [self.contentView addGestureRecognizer:recognizer];
    
    recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [self.contentView addGestureRecognizer:recognizer];
    

}

//左滑手势响应链
- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{

    if (!_canSelect) {
        if(recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
            [UIView animateKeyframesWithDuration:0.3
                                           delay:0.0
                                         options:UIViewKeyframeAnimationOptionLayoutSubviews
                                      animations:^{
                                          
                                          _baseView.frame = CGRectMake(-240, 0, _baseView.frame.size.width,  _baseView.frame.size.height);
                                      }
                                      completion:^(BOOL finished) {
                                          _ifhuadong = YES;
                                      }];
            
        }
        
        if(recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
            
            [self animationToidenty];
        }
    }
}

//右滑选择
- (void)swipeToRightForSelect{

    if (_canSelect) {
        [UIView animateKeyframesWithDuration:0.2
                                       delay:0.0
                                     options:UIViewKeyframeAnimationOptionLayoutSubviews
                                  animations:^{
                                      
                                      _baseView.frame = CGRectMake(40, 0, _baseView.frame.size.width,  _baseView.frame.size.height);
                                  }
                                  completion:^(BOOL finished) {
                                      
                                  }];
    }

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    if (_canSelect) {
        
        if ([self.delegate respondsToSelector:@selector(selectHeaderSection:)]) {
            [self.delegate selectHeaderSection:_section];
        }
    }else{
        [self animationToidenty];
    }
}

- (void)animationToidenty{

    [UIView animateKeyframesWithDuration:0.3
                                   delay:0.0
                                 options:UIViewKeyframeAnimationOptionLayoutSubviews
                              animations:^{
                                  
                                  _baseView.frame = CGRectMake(0, 0, _baseView.frame.size.width, _baseView.frame.size.height);
                              }
                              completion:^(BOOL finished) {
                                  _ifhuadong = NO;
                              }];

}

- (UIImageView *)seletImage{
    
    if (!_seletImage) {
        _seletImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
        _seletImage.backgroundColor = [UIColor clearColor];
        _seletImage.center = CGPointMake(25, (_titleHeight+57)/2);
    }
    return _seletImage;
}


@end
